<template>
<view class="uni-container">
			<uni-table ref="table" :loading="loading" border stripe type="selection" emptyText="暂无更多数据"
				>
				<uni-thead>
					<uni-tr>

						<uni-th width="150" align="center">
							&nbsp;</uni-th>
						<uni-th width="150" align="center">
							&nbsp;
						</uni-th>
						<uni-th align="center">
							<view @click="exportToExcel">导出Excel</view>
						</uni-th>
						<uni-th width="204" align="center">

							&nbsp;
						</uni-th>
					</uni-tr>

				</uni-thead>
				<uni-tr>
					<uni-th width="150" align="center">故障代码</uni-th>
					<uni-th width="150" align="center">描述</uni-th>
					<uni-th align="center">报警等级</uni-th>
					<uni-th width="204" align="center">异常处理方式</uni-th>
				</uni-tr>
				<uni-tr v-for="(item, index) in tableData" :key="index">
					<uni-td align="center">{{ item.name }}</uni-td>
					<uni-td align="center">
						<view class="name">{{ item.description }}</view>
					</uni-td>
					<uni-td align="center">{{ item.level }}</uni-td>
					<uni-td align="center">
						{{item.deal}}
					</uni-td>
				</uni-tr>




			</uni-table>
			<view class="uni-pagination-box"><uni-pagination show-icon :page-size="pageSize" :current="pageCurrent"
					:total="total" @change="change" /></view>
		</view>
</template>

<script>
	import axios from 'axios';
	import tableData from './tableData.js'
	import * as XLSX from 'xlsx';
	import {
		saveAs
	} from 'file-saver';
	export default {
		data() {
			return {
				searchVal: '',
				typeData: "../../err.json",//错误代码
				selectedIndexs:[],
				tableData: [],
				// 每页数据量
				pageSize: 15,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				loading: false,
				groupData:[],
			}
		},
		onLoad() {
			this.selectedIndexs = []
		},
		created() {
			var that = this;
			this.readData(function(data) {
				that.getData(1)
			});
		},
		computed: {

		},
		methods: {
			readData(callback) {
				var that = this;
				axios.get(this.typeData)
					.then(response => {

						this.groupData = response.data.items;
						//分组数据
						this.tableData = [];
						response.data.items.forEach(function(element) {
							var it = {
								"name" : element.name,
								    "description" : element.description,
								    "break_type" : element.break_type,
									"allow_repair" : element.allow_repair,
									"routing_loop" : element.routing_loop,
									"qc_loop" : element.qc_loop,
							};
							it=that.getDeal(it);
							that.tableData.push(it);
							
						}, this);
						if (typeof callback != 'undefined') {
							setTimeout(() => {
								this.groupData=this.tableData;//暂存
								callback(this.tableData);
							}, 500)

						}
					})
					.catch(error => {
						console.log(error);
					});
			},
			getDeal(element){
				if(element.qc_loop){
					//A
					element.level='A(1)';
					element.deal='锁机';
				}else if(element.routing_loop){
					//B
					element.level='B(2)';
					element.deal='锁批';
					
				}else if(element.allow_repair){
					//C
					element.level='C(3)';
					element.deal='消息通知';
				}else{
					console.log(element);
					//C -
					element.level='C(3)';
					element.deal='无';
				}
				return element;
			},
			// 多选处理
			selectedItems() {
				return this.selectedIndexs.map(i => this.tableData[i])
			},
			// 多选
			selectionChange(e) {
				console.log(e.detail.index)
				this.selectedIndexs = e.detail.index
			},
			//批量删除
			delTable() {
				console.log(this.selectedItems())
			},
			// 分页触发
			change(e) {
				this.$refs.table.clearSelection()
				this.selectedIndexs.length = 0
				this.getData(e.current)
			},
			// 搜索
			search() {
				this.getData(1, this.searchVal)
			},
			// 获取数据
			getData(pageCurrent, value = '') {
				this.loading = true
				this.pageCurrent = pageCurrent
				this.request({
					pageSize: this.pageSize,
					pageCurrent: pageCurrent,
					value: value,
					success: res => {
						// console.log('data', res);
			
						this.tableData = res.data
						this.total = res.total
						this.loading = false
					}
				})
			},
			// 伪request请求
			request(options) {
				const {
					pageSize,
					pageCurrent,
					success,
					value
				} = options
				var tempArr = this.groupData;
				let total = tempArr.length
				let data = tempArr.filter((item, index) => {
					const idx = index - (pageCurrent - 1) * pageSize
					return idx < pageSize && idx >= 0
				})
				if (value) {
					data = []
					tempArr.forEach(item => {
						if (item.name.indexOf(value) !== -1) {
							data.push(item)
						}
					})
					total = data.length
				}
			
				setTimeout(() => {
					typeof success === 'function' &&
						success({
							data: data,
							total: total
						})
				}, 500)
			},
			//导出excel
			exportToExcel() {
				// 假设你有一个表格数据的数组
			
				var filename = "故障代码";
				 var tempArr = this.groupData;
				var data = [
					["故障代码", "描述", "报警等级","异常处理方式"],
				];
				for (var i = 0; i < tempArr.length; i++) {
					data.push([tempArr[i].name, tempArr[i].description, tempArr[i].level,tempArr[i].deal]);
				}
			
				// 将数据转换为工作表
				const worksheet = XLSX.utils.aoa_to_sheet(data);
			
				// 创建工作簿并添加工作表
				const workbook = XLSX.utils.book_new();
				XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
			
				// 生成Excel文件
				const excelBuffer = XLSX.write(workbook, {
					bookType: 'xlsx',
					type: 'array'
				});
			
				// 使用blob和FileReader创建一个Blob URL
				const dataBlob = new Blob([excelBuffer], {
					type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
				});
				const blobUrl = window.URL.createObjectURL(dataBlob);
			
				// 使用saveAs下载文件
				saveAs(dataBlob,  filename + 'export' + (new Date().toLocaleDateString()) + '.xlsx');
			
				// 清理
				window.URL.revokeObjectURL(blobUrl);
			},
		}
	}
</script>

<style>
	/* #ifndef H5 */
	/* page {
	padding-top: 85px;
} */
	/* #endif */
	.uni-group {
		display: flex;
		align-items: center;
	}
</style>